package gruppoC;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;


public class Servizio0 implements IServizio{
	
	private ArrayList<String> requestedContent = new ArrayList<String>();

	@Override
	public ArrayList<String> getAnswer(String request) {
		
		Collections.addAll(requestedContent, "HTTP/1.1 200 OK", 
				"Date: Tue, 01 Apr 2014 08:41:20",
				"Content-Type: text/html; charset=utf-8",
				"");
		

//	    System.out.println(request);
	    
	    openRequestedFile(request);
    
		return requestedContent;
	}
	
	private void openRequestedFile(String requestedFile) {

		try {
			File file = new File("web"+requestedFile);
			BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
			
			while (bufferedReader.ready()) {
				requestedContent.add(bufferedReader.readLine());
			}
			
			bufferedReader.close();
		} catch (FileNotFoundException e) {
			openRequestedFile("/error.html");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

}
